using System;
using System.Collections.Generic;

namespace Dida
{
	public interface IObjectManager
	{
		object GetObject(string name);
		object GetObject(string name, Type type);
	}

	public class ObjectManager : IObjectManager
	{
		object[] objects;
		Dictionary<string, int> symbols;

		public ObjectManager(object[] objects,Dictionary<string,int> symbols)
		{
			this.objects = objects;
			this.symbols = symbols;
		}

		public object GetObject(string name)
		{
			int index;
			if (symbols.TryGetValue(name, out index))
				return objects[index];
			else
				throw new Exception("There's no object called \"" + name + "\" (or it's not exported).");
		}

		public object GetObject(string name, Type type)
		{
			object ret = GetObject(name);
			if (!type.IsInstanceOfType(ret))
			{
				throw new Exception("The object \"" + name + "\" isn't a " + 
					type.Name + ".");
			}
			return ret;
		}

		public object GetObject(int index)
		{
			return objects[index];
		}

		public object GetObject(int index, Type type)
		{
			object ret = objects[index];
			if (!type.IsInstanceOfType(ret))
			{
				throw new Exception("The object with index " + index.ToString() + " isn't a " +
					type.Name + ".");
			}
			return ret;
		}
	}
}
